package com.briup.es.web;

import com.briup.es.auth.SecurityUserContext;
import com.briup.es.config.response.Result;
import com.briup.es.domain.bean.Shopcar;
import com.briup.es.domain.vo.ShopCarGoodsVO;
import com.briup.es.service.ShopcarService;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@RequestMapping("/shopCar")
@RequiredArgsConstructor
public class ShopCarController {

    private final ShopcarService service;

    @GetMapping("/")
    public Result<List<ShopCarGoodsVO>> queryList() {
        List<ShopCarGoodsVO> goodsVOS = service.queryList(SecurityUserContext.currentUserId());
        return Result.success(goodsVOS);
    }

    @PutMapping("/{bookId}")
    public Result<?> addShopCar(@PathVariable Integer bookId) {
        service.addShopCar(bookId);
        return Result.success();
    }

    @PutMapping("/{id}/{num}")
    public Result<?> updateShopcar(@PathVariable Integer id,
                                   @PathVariable @Min(value = 1, message = "图书至少为1才能加入购物车") Integer num) {
        service.updateShopcar(id, num);
        return Result.success();
    }

    @DeleteMapping("/")
    public Result<?> deleteShopcar(@RequestBody List<Integer> ids) {
        service.deleteShopcar(ids);
        return Result.success();
    }

    @PostMapping("/queryByIds")
    public Result<List<Shopcar>> queryByIds(@RequestBody List<Integer> ids) {
        List<Shopcar> shopcars = service.listByIds(ids);
        return Result.success(shopcars);
    }
}